﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.MEP;
using YArchitech.Plumbing;
using HYFamilyLibDataManager;

namespace YArchitech.HVAC
{
	public partial class FormLayFan : System.Windows.Forms.Form
	{
		public object ControlJudgement { get; private set; }

		public FormLayFan(Autodesk.Revit.DB.Document doc, List<Family> listDuctFan, List<Family> listOtherFan)
		{
			this.listInfos = this.sfdr.GetAllNameAndImage("Fan");
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			this.doc = doc;
			base.Location = new System.Drawing.Point(100, 100);
			base.Icon = Resource.YArchitechIcon;
			this.listDuctFan = listDuctFan;
			this.listOtherFan = listOtherFan;
			this.initParameterSetting();
			this.lbfname.Text = this.name;
			if (this.listInfos.Count > 0)
			{
				foreach (ListItem listItem in this.listInfos)
				{
					if (listItem.Name == this.name)
					{
						this.GetParasFromDB(listItem);
						this.id = int.Parse(listItem.Tag.ToString());
						break;
					}
				}
				this.lbSymbol.SelectedItem = this.familySymbol;
			}
			if (!this.chbLayDuct.Checked)
			{
				this.chbLayFlex.Checked = false;
				this.chbLayFlex.Enabled = false;
			}
			this.SetLabelLocation();
		}

		private void SetLabelLocation()
		{
			int x = this.pictureBox1.Location.X;
			int y = this.pictureBox1.Location.Y;
			int width = this.pictureBox1.Size.Width;
			int height = this.pictureBox1.Size.Height;
			int width2 = this.lbfname.Size.Width;
			int height2 = this.lbfname.Size.Height;
			int num = (width - width2) / 2;
			int num2 = height + height2 / 2;
			this.lbfname.Location = new System.Drawing.Point(x + num, y + num2);
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			if (this.tabHY.SelectedIndex == 0)
			{
				this.HYLayFan();
				this.hongye = true;
				return;
			}
			this.CurrentProjectLayFan();
			this.hongye = false;
		}

		private void CurrentProjectLayFan()
		{
			if (!Regex.IsMatch(this.hyTxtCurrentRealativeLevel.Text, "^[+-]?[0-9]+\\d*[.]?\\d*$"))
			{
				YJKMessageBox.Show("相对标高必须为有效数字！");
				this.hyTxtCurrentRealativeLevel.Focus();
				return;
			}
			if (!Regex.IsMatch(this.hyTxtCurrentConnectLength.Text, "^[+-]?[0-9]+\\d*[.]?\\d*$"))
			{
				YJKMessageBox.Show("软管长度必须为有效数字！");
				this.hyTxtCurrentConnectLength.Focus();
				return;
			}
			if (Convert.ToDouble(this.hyTxtCurrentConnectLength.Text) == 0.0 && this.hyTxtCurrentConnectLength.Enabled)
			{
				YJKMessageBox.Show("软管长度不能为0！");
				this.hyTxtCurrentConnectLength.Focus();
				return;
			}
			if (this.fs == null)
			{
				YJKMessageBox.Show("您未选择当前项目中的族！");
				return;
			}
			bool flag = this.SetParam();
			if (this.fanParam != null && flag)
			{
				if (this.chkCurrentOnDuct.Enabled && this.chkCurrentOnDuct.Checked)
				{
					this.bLayDuct = true;
				}
				else
				{
					this.bLayDuct = false;
				}
				if (this.chkCurrentHoseConnect.Enabled && this.chkCurrentHoseConnect.Checked)
				{
					this.bLayFlex = true;
				}
				else
				{
					this.bLayFlex = false;
				}
				base.DialogResult = DialogResult.OK;
			}
		}

		private void HYLayFan()
		{
			if (!Regex.IsMatch(this.tbLength.Text, "^[+-]?[0-9]+\\d*[.]?\\d*$"))
			{
				YJKMessageBox.Show("风机长度必须为有效数字！");
				this.tbLength.Focus();
				return;
			}
			if (!Regex.IsMatch(this.tbWidth.Text, "^[+-]?[0-9]+\\d*[.]?\\d*$"))
			{
				YJKMessageBox.Show("风机宽度必须为有效数字！");
				this.tbWidth.Focus();
				return;
			}
			if (!Regex.IsMatch(this.tbHeight.Text, "^[+-]?[0-9]+\\d*[.]?\\d*$"))
			{
				YJKMessageBox.Show("风机高度必须为有效数字！");
				this.tbHeight.Focus();
				return;
			}
			if (!Regex.IsMatch(this.tbDiameter.Text, "^[+-]?[0-9]+\\d*[.]?\\d*$"))
			{
				YJKMessageBox.Show("风机直径必须为有效数字！");
				this.tbDiameter.Focus();
				return;
			}
			if (!Regex.IsMatch(this.tblevel.Text, "^[+-]?[0-9]+\\d*[.]?\\d*$"))
			{
				YJKMessageBox.Show("相对标高必须为有效数字！");
				this.tblevel.Focus();
				return;
			}
			if (!Regex.IsMatch(this.tbFlexLength.Text, "^[+-]?[0-9]+\\d*[.]?\\d*$"))
			{
				YJKMessageBox.Show("软管长度必须为有效数字！");
				this.tbFlexLength.Focus();
				return;
			}
			if (Convert.ToDouble(this.tbFlexLength.Text) == 0.0 && this.tbFlexLength.Enabled)
			{
				YJKMessageBox.Show("软管长度不能为0！");
				this.tbFlexLength.Focus();
				return;
			}
			bool waterTankParamFromForm = this.GetWaterTankParamFromForm();
			if (this.fanParam != null && waterTankParamFromForm)
			{
				if (this.chbLayDuct.Enabled && this.chbLayDuct.Checked)
				{
					this.bLayDuct = true;
				}
				else
				{
					this.bLayDuct = false;
				}
				if (this.chbLayFlex.Enabled && this.chbLayFlex.Checked)
				{
					this.bLayFlex = true;
				}
				else
				{
					this.bLayFlex = false;
				}
				base.DialogResult = DialogResult.OK;
			}
		}

		private void btnCancle_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
		}

		private void pictureBox1_Click(object sender, EventArgs e)
		{
			FormChooseHydrant formChooseHydrant = new FormChooseHydrant(this.name, this.listInfos);
			formChooseHydrant.Text = "选择风机";
			formChooseHydrant.Owner = this;
			if (formChooseHydrant.ShowDialog() == DialogResult.OK && this.name != formChooseHydrant.ImageName && formChooseHydrant.ImageName != "")
			{
				this.name = formChooseHydrant.ImageName;
				this.lbfname.Text = this.name;
				foreach (ListItem listItem in this.listInfos)
				{
					if (listItem.Name == this.name)
					{
						this.GetParasFromDB(listItem);
						this.id = int.Parse(listItem.Tag.ToString());
						break;
					}
				}
			}
		}

		private void GetParasFromDB(ListItem item)
		{
			List<ListItem> infoById = this.sfdr.GetInfoById("Fan", int.Parse(item.Tag.ToString()));
			if (infoById.Count > 0)
			{
				foreach (ListItem listItem in infoById)
				{
					if (listItem.Name == "Image")
					{
						if (File.Exists(listItem.Value))
						{
							this.pictureBox1.Image = new Bitmap(listItem.Value);
						}
						else
						{
							this.pictureBox1.Image = null;
						}
					}
					else if (listItem.Name == "FamilySymbol")
					{
						this.UptadaFamilySybol(listItem.Value);
					}
				}
			}
		}

		private void UptadaFamilySybol(string szData)
		{
			this.lbSymbol.Items.Clear();
			foreach (string text in szData.Split(new char[]
			{
				';',
				',',
				'，'
			}))
			{
				if (text.Trim() != "")
				{
					this.lbSymbol.Items.Add(text);
				}
			}
			if (this.lbSymbol.Items != null && this.lbSymbol.Items.Count > 0)
			{
				this.lbSymbol.SelectedIndex = 0;
			}
		}

		private void initParameterSetting()
		{
			this.tvwHY.Nodes.Add("机械设备");
			this.btnMore.Enabled = false;
			this.btnMore.Visible = false;
			HVACUserSetting hvacuserSetting = new HVACUserSetting();
			Dictionary<string, string> fansLaySetting = hvacuserSetting.GetFansLaySetting();
			this.tabHY.SelectedIndex = Convert.ToInt32(hvacuserSetting.GetValueFromDic("HYDuctFan", "0", fansLaySetting));
			if (Convert.ToBoolean(hvacuserSetting.GetValueFromDic("CurrentFanType", "true", fansLaySetting)))
			{
				this.rdoDuctFan.Checked = true;
				this.LayDuctFan();
			}
			else
			{
				this.rdoOtherFan.Checked = true;
				this.LayOtherFan();
			}
			this.hyTxtCurrentRealativeLevel.Text = hvacuserSetting.GetValueFromDic("CurrentRelativeLevel", "0.00", fansLaySetting);
			this.hyTxtCurrentConnectLength.Text = hvacuserSetting.GetValueFromDic("CurrentConnectLength", "200", fansLaySetting);
			this.name = hvacuserSetting.GetValueFromDic("familyName", "屋顶风机-离心式", fansLaySetting);
			this.familySymbol = hvacuserSetting.GetValueFromDic("familySymbol", "标准", fansLaySetting);
			this.tblevel.Text = hvacuserSetting.GetValueFromDic("offset", "0.00", fansLaySetting);
			this.tbWidth.Text = hvacuserSetting.GetValueFromDic("width", "500", fansLaySetting);
			this.tbHeight.Text = hvacuserSetting.GetValueFromDic("height", "500", fansLaySetting);
			this.tbLength.Text = hvacuserSetting.GetValueFromDic("length", "600", fansLaySetting);
			this.tbFlexLength.Text = hvacuserSetting.GetValueFromDic("flexLength", "200", fansLaySetting);
			this.tbDiameter.Text = hvacuserSetting.GetValueFromDic("diameter", "500", fansLaySetting);
			this.chbLayDuct.Checked = Convert.ToBoolean(hvacuserSetting.GetValueFromDic("layduct", "False", fansLaySetting));
			this.chbLayFlex.Checked = Convert.ToBoolean(hvacuserSetting.GetValueFromDic("layflex", "False", fansLaySetting));
		}

		private void LayOtherFan()
		{
			this.fanType = false;
			this.hyTxtCurrentRealativeLevel.Enabled = true;
			this.hyTxtCurrentConnectLength.Enabled = false;
			this.chkCurrentOnDuct.Checked = false;
			this.chkCurrentHoseConnect.Checked = false;
			this.chkCurrentHoseConnect.Enabled = false;
			this.chkCurrentOnDuct.Enabled = false;
			HyProjectFamily.BingTvwFamilys(this.listOtherFan, this.tvwHY);
		}

		private void LayDuctFan()
		{
			this.fanType = true;
			this.hyTxtCurrentRealativeLevel.Enabled = false;
			this.hyTxtCurrentConnectLength.Enabled = true;
			this.chkCurrentHoseConnect.Enabled = true;
			this.chkCurrentOnDuct.Enabled = true;
			this.chkCurrentOnDuct.Checked = true;
			this.chkCurrentHoseConnect.Checked = true;
			HyProjectFamily.BingTvwFamilys(this.listDuctFan, this.tvwHY);
		}

		private void UserSettingWrite()
		{
			this.tblevel.Text = StringTools.RemoveZero(this.tblevel.Text.Trim());
			try
			{
				new HVACUserSetting().WriteFansLaySetting(new Dictionary<string, string>
				{
					{
						"HYDuctFan",
						this.tabHY.SelectedIndex.ToString()
					},
					{
						"CurrentFanType",
						this.fanType.ToString()
					},
					{
						"CurrentRelativeLevel",
						this.hyTxtCurrentRealativeLevel.Text
					},
					{
						"CurrentConnectLength",
						this.hyTxtCurrentConnectLength.Text.ToString()
					},
					{
						"familyName",
						this.name
					},
					{
						"familySymbol",
						this.familySymbol
					},
					{
						"offset",
						this.tblevel.Text
					},
					{
						"layduct",
						this.chbLayDuct.Checked.ToString()
					},
					{
						"layflex",
						this.chbLayFlex.Checked.ToString()
					},
					{
						"width",
						this.tbWidth.Text.ToString()
					},
					{
						"height",
						this.tbHeight.Text.ToString()
					},
					{
						"length",
						this.tbLength.Text.ToString()
					},
					{
						"flexLength",
						this.tbFlexLength.Text.ToString()
					},
					{
						"diameter",
						this.tbDiameter.Text.ToString()
					}
				});
			}
			catch (Exception)
			{
			}
		}

		public bool GetWaterTankParamFromForm()
		{
			new List<System.Windows.Forms.Control>().Add(this.tblevel);
			this.fanParam = new EquipmentFanParam2();
			this.fanParam.OffSet = double.Parse(this.tblevel.Text.Trim());
			this.fanParam.SymbolName = this.lbSymbol.Text.Trim();
			this.fanParam.Width = double.Parse(this.tbWidth.Text.Trim());
			this.fanParam.Height = double.Parse(this.tbHeight.Text.Trim());
			this.fanParam.Length = double.Parse(this.tbLength.Text.Trim());
			this.fanParam.FlexLength = double.Parse(this.tbFlexLength.Text.Trim());
			this.fanParam.Diameter = double.Parse(this.tbDiameter.Text.Trim());
			this.fanParam.FamilyName = this.lbfname.Text.Trim();
			this.fanParam.Id = this.id;
			return true;
		}

		private bool SetParam()
		{
			this.fanParam = new EquipmentFanParam2();
			this.fanParam.OffSet = double.Parse(this.hyTxtCurrentRealativeLevel.Text.Trim());
			this.fanParam.FlexLength = double.Parse(this.hyTxtCurrentConnectLength.Text.Trim());
			this.fanParam.Length = double.Parse(this.tbLength.Text.Trim());
			this.fanParam.SymbolName = this.fs.Name;
			this.fanParam.Diameter = double.Parse(this.tbDiameter.Text.Trim());
			this.fanParam.FamilyName = this.fs.Family.Name.Trim();
			return true;
		}

		private void tblevel_Validated(object sender, EventArgs e)
		{
		}

		private void btnMore_Click(object sender, EventArgs e)
		{
			if (FamilyUtil.PathIni.ReadPath(this.doc.Application.VersionNumber) == "")
			{
				YJKMessageBox.Show("请先安装盈建科族立得。");
				return;
			}
			LayFamilyForm layFamilyForm = new LayFamilyForm(new HVACDataReader(this.doc).GetFamilyAllInfoTree("风机"), this.doc);
			if (layFamilyForm.ShowDialog() == DialogResult.OK)
			{
				this.fs = layFamilyForm.fs;
				this.fanParam = new EquipmentFanParam2();
				this.fanParam.OffSet = layFamilyForm.OffSet;
				base.DialogResult = DialogResult.OK;
			}
		}

		private void lbfname_TextChanged(object sender, EventArgs e)
		{
			if (this.lbfname.Text.Equals("轴流式管道风机"))
			{
				this.tbLength.Enabled = true;
				this.tbWidth.Enabled = false;
				this.tbHeight.Enabled = false;
				this.tbDiameter.Enabled = true;
				if (this.chbLayDuct.Checked)
				{
					this.tblevel.Enabled = false;
				}
				else
				{
					this.tblevel.Enabled = true;
				}
				this.chbLayDuct.Enabled = true;
				this.chbLayFlex.Enabled = true;
				this.tbFlexLength.Enabled = true;
			}
			else if (this.lbfname.Text.Equals("离心式管道风机"))
			{
				this.tbLength.Enabled = true;
				this.tbWidth.Enabled = true;
				this.tbHeight.Enabled = true;
				this.tbDiameter.Enabled = false;
				if (this.chbLayDuct.Checked)
				{
					this.tblevel.Enabled = false;
				}
				else
				{
					this.tblevel.Enabled = true;
				}
				this.chbLayDuct.Enabled = true;
				this.chbLayFlex.Enabled = true;
				this.tbFlexLength.Enabled = true;
			}
			else if (this.lbfname.Text.Equals("方形轴流风机"))
			{
				this.tbLength.Enabled = true;
				this.tbWidth.Enabled = true;
				this.tbHeight.Enabled = false;
				this.tbDiameter.Enabled = false;
				this.tblevel.Enabled = true;
				this.chbLayDuct.Enabled = false;
				this.chbLayFlex.Enabled = false;
				this.tbFlexLength.Enabled = false;
				this.chbLayDuct.Checked = false;
			}
			else if (this.lbfname.Text.Equals("屋顶风机-离心式"))
			{
				this.tbLength.Enabled = false;
				this.tbWidth.Enabled = false;
				this.tbHeight.Enabled = false;
				this.tbDiameter.Enabled = true;
				this.tblevel.Enabled = true;
				this.chbLayDuct.Enabled = false;
				this.chbLayFlex.Enabled = false;
				this.tbFlexLength.Enabled = false;
				this.chbLayDuct.Checked = false;
			}
			else if (this.lbfname.Text.Equals("屋顶风机-轴流式"))
			{
				this.tbLength.Enabled = false;
				this.tbWidth.Enabled = false;
				this.tbHeight.Enabled = false;
				this.tbDiameter.Enabled = true;
				this.tblevel.Enabled = true;
				this.chbLayDuct.Enabled = false;
				this.chbLayFlex.Enabled = false;
				this.tbFlexLength.Enabled = false;
				this.chbLayDuct.Checked = false;
			}
			if (!this.chbLayDuct.Checked)
			{
				this.chbLayFlex.Checked = false;
				this.chbLayFlex.Enabled = false;
			}
		}

		private void chbLayDuct_CheckedChanged(object sender, EventArgs e)
		{
			if (this.chbLayDuct.Checked && this.chbLayDuct.Enabled)
			{
				this.tblevel.Enabled = false;
				this.chbLayFlex.Enabled = true;
				this.chbLayFlex.Checked = true;
				return;
			}
			this.tblevel.Enabled = true;
			this.chbLayFlex.Enabled = false;
			this.chbLayFlex.Checked = false;
		}

		private void chbLayFlex_CheckedChanged(object sender, EventArgs e)
		{
			if (this.chbLayFlex.Checked && this.chbLayFlex.Enabled)
			{
				this.tbFlexLength.Enabled = true;
				return;
			}
			this.tbFlexLength.Enabled = false;
		}

		private void rdoDuctFan_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdoDuctFan.Checked)
			{
				this.LayDuctFan();
				return;
			}
			this.LayOtherFan();
		}

		private void tvwHY_AfterSelect(object sender, TreeViewEventArgs e)
		{
			this.fs = null;
			if (this.tvwHY.SelectedNode == null)
			{
				return;
			}
			if (this.tvwHY.SelectedNode.Level == 1)
			{
				this.fs = (this.tvwHY.SelectedNode.Nodes[0].Tag as FamilySymbol);
			}
			else if (this.tvwHY.SelectedNode.Level == 2)
			{
				this.fs = (this.tvwHY.SelectedNode.Tag as FamilySymbol);
			}
			this.tvwHY.SelectedNode.BackColor = System.Drawing.Color.DodgerBlue;
			this.tvwHY.SelectedNode.ForeColor = System.Drawing.Color.White;
		}

		private void tvwHY_BeforeSelect(object sender, TreeViewCancelEventArgs e)
		{
			if (this.tvwHY.SelectedNode != null)
			{
				this.tvwHY.SelectedNode.BackColor = System.Drawing.Color.White;
				this.tvwHY.SelectedNode.ForeColor = System.Drawing.Color.Black;
			}
		}

		private void FormLayFan_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.UserSettingWrite();
		}

		private void chkCurrentOnDuct_CheckedChanged(object sender, EventArgs e)
		{
			if (this.chkCurrentOnDuct.Checked && this.chkCurrentOnDuct.Enabled)
			{
				this.hyTxtCurrentRealativeLevel.Enabled = false;
				this.chkCurrentHoseConnect.Enabled = true;
				this.chkCurrentHoseConnect.Checked = true;
				this.hyTxtCurrentConnectLength.Enabled = true;
				return;
			}
			this.hyTxtCurrentRealativeLevel.Enabled = true;
			this.chkCurrentHoseConnect.Enabled = false;
			this.chkCurrentHoseConnect.Checked = false;
			this.hyTxtCurrentConnectLength.Enabled = false;
		}

		private void tabHY_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.tabHY.SelectedIndex == 0)
			{
				this.hongye = true;
				return;
			}
			this.hongye = false;
		}

		private void chkCurrentHoseConnect_CheckedChanged(object sender, EventArgs e)
		{
			if (this.chkCurrentHoseConnect.Checked)
			{
				this.hyTxtCurrentConnectLength.Enabled = true;
				return;
			}
			this.hyTxtCurrentConnectLength.Enabled = false;
		}

		private SpaceFamilyDataReader sfdr = new SpaceFamilyDataReader();

		private List<ListItem> listInfos = new List<ListItem>();

		private UserSetting userSetting = new UserSetting();

		public EquipmentFanParam2 fanParam;

		private string name = "";

		public int id;

		private string familySymbol = "";

		public FamilySymbol fs;

		public bool bLayDuct;

		public bool bLayFlex;

		private Document doc;

		public bool hongye = true;

		private List<Family> listDuctFan = new List<Family>();

		private List<Family> listOtherFan = new List<Family>();

		private bool fanType = true;
	}
}
